﻿using System.Linq;
using System.Web.Mvc;
using StudyTracker.Domain.Entities;
using StudyTracker.Web.Models.Data;

namespace StudyTracker.Web.Controllers
{
	public partial class DataController : BaseController
	{
		public virtual ViewResult DailyGraph()
		{
			ViewBag.Title = GraphModel.DailyGraphTitle;
			return View(MVC.Data.Views.Graph, new GraphModel(CurrentStudent, GraphType.Daily));
		}

		public virtual ViewResult TotalGraph()
		{
			ViewBag.Title = GraphModel.TotalGraphTitle;
			return View(MVC.Data.Views.Graph, new GraphModel(CurrentStudent, GraphType.Total));
		}

		[ChildActionOnly]
		[HttpGet]
		public virtual ActionResult AddSingle()
		{
			return PartialView(new AddSingleModel(CurrentStudent));
		}

		[HttpPost]
		public virtual ActionResult AddSingle(AddSingleModel model, string returnUrl)
		{
			if (ModelState.IsValid)
			{
				Student.Subject subject = CurrentStudent.Subjects.Where(x => x.Name == model.Subject).SingleOrDefault();

				if (subject == null)
				{
					subject = new Student.Subject(model.Subject);
					CurrentStudent.Subjects.Add(subject);
				}

				subject.StudySessions.Add(new Student.Subject.StudySession(model.Amount.Value));
				Student.Save(CurrentStudent);
			}

			return Redirect(returnUrl);
		}
	}
}
